package com.work.straighttalk.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hyphenate.chat.EMMessage;
import com.work.straighttalk.R;
import com.work.straighttalk.adapter.ContactsRVAdapter;
import com.work.straighttalk.adapter.OnRecyclerViewClickListener;
import com.work.straighttalk.app.BaseFragment;
import com.work.straighttalk.bean.Contact;
import com.work.straighttalk.presenter.ContactsLoadDataPresenterImpl;
import com.work.straighttalk.presenter.i.LoadDataPresenterI;
import com.work.straighttalk.ui.ChatActivity;
import com.work.straighttalk.ui.i.BindDataVI;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 16/3/1.
 */
public class ContactsFragment extends BaseFragment implements BindDataVI<List<Contact>>, OnRecyclerViewClickListener {
    @Bind(R.id.rv_contact)
    RecyclerView rvContact;
    private ContactsRVAdapter contactsRVAdapter;
    private LoadDataPresenterI loadDataPresenterI;

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main_toolbar_conversation, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        contactsRVAdapter = new ContactsRVAdapter();
        rvContact.setLayoutManager(new LinearLayoutManager(getContext()));
        rvContact.setAdapter(contactsRVAdapter);
        contactsRVAdapter.setListener(this);
        loadDataPresenterI = new ContactsLoadDataPresenterImpl(this);
        loadDataPresenterI.loadData(null);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void bindData(List<Contact> contacts) {
        contactsRVAdapter.loadData(contacts);
    }

    @Override
    public void onRecyclerViewCLick(Object o) {
        String username = (String) o;
        Toast.makeText(getContext(), username, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getContext(), ChatActivity.class);
        intent.putExtra("userID", username);
        startActivity(intent);
    }


}
